/*
 * Copyright (c) 2012 Johan Fylling
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights 
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 * THE SOFTWARE.
 */

part of tools;


class Keyboard extends InputMethod {
  static const String INPUT_TYPE = "Keyboard";
  
  static const int KEYS          = 255;
  
  static const int KEY_BACKSPACE = 8;
  static const int KEY_TAB       = 9;
  static const int KEY_ENTER     = 13;
  static const int KEY_SHIFT     = 16;
  static const int KEY_CTRL      = 17;
  static const int KEY_ALT       = 18;
  static const int KEY_PAUSE     = 19;
  static const int KEY_CAPS      = 20;
  static const int KEY_ESC       = 27;
  static const int KEY_SPACE     = 32;
  static const int KEY_PAGE_UP   = 33;
  static const int KEY_PAGE_DOWN = 34;
  static const int KEY_END       = 35;
  static const int KEY_HOME      = 36;
  static const int KEY_LEFT      = 37;
  static const int KEY_UP        = 38;
  static const int KEY_RIGHT     = 39;
  static const int KEY_DOWN      = 40;
  static const int KEY_0         = 48;
  static const int KEY_1         = 49;
  static const int KEY_2         = 50;
  static const int KEY_3         = 51;
  static const int KEY_4         = 52;
  static const int KEY_5         = 53;
  static const int KEY_6         = 54;
  static const int KEY_7         = 55;
  static const int KEY_8         = 56;
  static const int KEY_9         = 57;
  static const int KEY_A         = 65;
  static const int KEY_B         = 66;
  static const int KEY_C         = 67;
  static const int KEY_D         = 68;
  static const int KEY_E         = 69;
  static const int KEY_F         = 70;
  static const int KEY_G         = 71;
  static const int KEY_H         = 72;
  static const int KEY_I         = 73;
  static const int KEY_J         = 74;
  static const int KEY_K         = 75;
  static const int KEY_L         = 76;
  static const int KEY_M         = 77;
  static const int KEY_N         = 78;
  static const int KEY_O         = 79;
  static const int KEY_P         = 80;
  static const int KEY_Q         = 81;
  static const int KEY_R         = 82;
  static const int KEY_S         = 83;
  static const int KEY_T         = 84;
  static const int KEY_U         = 85;
  static const int KEY_V         = 86;
  static const int KEY_W         = 87;
  static const int KEY_X         = 88;
  static const int KEY_Y         = 89;
  static const int KEY_Z         = 90;
  static const int KEY_NUMPAD_0  = 96;
  static const int KEY_NUMPAD_1  = 97;
  static const int KEY_NUMPAD_2  = 98;
  static const int KEY_NUMPAD_3  = 99;
  static const int KEY_NUMPAD_4  = 100;
  static const int KEY_NUMPAD_5  = 101;
  static const int KEY_NUMPAD_6  = 102;
  static const int KEY_NUMPAD_7  = 103;
  static const int KEY_NUMPAD_8  = 104;
  static const int KEY_NUMPAD_9  = 105;
  static const int KEY_MULTIPLY  = 106;
  static const int KEY_ADD       = 107;
  static const int KEY_SUBTRACT  = 109;
  static const int KEY_DECIMAL   = 110;
  static const int KEY_DIVIDE    = 111;
  static const int KEY_F1        = 112;
  static const int KEY_F2        = 113;
  static const int KEY_F3        = 114;
  static const int KEY_F4        = 115;
  static const int KEY_F5        = 116;
  static const int KEY_F6        = 117;
  static const int KEY_F7        = 118;
  static const int KEY_F8        = 119;
  static const int KEY_F9        = 120;
  static const int KEY_F10       = 121;
  static const int KEY_F11       = 122;
  static const int KEY_F12       = 123;
  
  Keyboard () : super(KEYS) {
    window.onKeyDown.listen(this._onKeyDown);
    window.onKeyPress.listen(this._onKeyPress);
    window.onKeyUp.listen(this._onKeyUp);
  }
  
  void _onKeyDown (KeyboardEvent event) {
    this.stateSet[event.keyCode] = true;
  }
  
  void onKeyDown (int key, callback()) {
    window.onKeyDown.listen((KeyboardEvent event) {
      if (event.keyCode == key) {
        callback();
      }
    });
  }
  
  void _onKeyPress (KeyboardEvent event) {
    this.stateSet[event.keyCode] = true;
  }
  
  void onKeyPress (int key, callback()) {
    window.onKeyPress.listen((KeyboardEvent event) {
      if (event.keyCode == key) {
        callback();
      }
    });
  }
  
  void _onKeyUp (KeyboardEvent event) {
    this.stateUnset.add(event.keyCode);
  }
  
  void onKeyUp (int key, callback()) {
    window.onKeyUp.listen((KeyboardEvent event) {
      if (event.keyCode == key) {
        callback();
      }
    });
  }
}
